package com.ldj.struts.action;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UserManagerScope;


public class SysLogAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SysLogInfoDAO dao = new SysLogInfoDAO(request, response);
		String begintime = request.getParameter("begintime");
		String endtime = request.getParameter("endtime");
		String type = request.getParameter("sysLogInfoOperType");
		String code = request.getParameter("code");
		List<SysLogInfo> list= (List<SysLogInfo>)dao.findAllPage(begintime,endtime,type,code);
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	//有权限机构树
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String code = request.getParameter("code");
		String value = "";
		PersionFileDAO dao = new PersionFileDAO(request,response);
		List<UserManagerScope> infoList = dao.findBySuperviseOrganId(SystemSession.getSupUserId(request));
		if(infoList != null && infoList.size()>0){
			UserManagerScope info1 =infoList.get(0);
			value = info1.getUserManagerScopeOrganId();
		}
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getPowerSuperviseOrganTree(request, response, "监察机构", "", "SysLog.do?method=list&code=$code", value));		
		return mapping.findForward("tree");
	}
	
	public ActionForward downloadExcel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws WriteException, SQLException, IOException {
			
		Calendar rightNow = Calendar.getInstance();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		SysLogInfoDAO dao = new SysLogInfoDAO(request, response);
        
        String begintime = request.getParameter("begintime");
 		String endtime = request.getParameter("endtime");
 		String type = request.getParameter("sysLogInfoOperType");
		List<SysLogInfo> list= (List<SysLogInfo>)dao.findAll(begintime,endtime,type);
	    String today = fmt.format(rightNow.getTime());
	    StringBuffer sb = new StringBuffer(today + "导出的日志\r\n");
	    if(list != null && list.size() > 0){
	    	for(int i = 0; i < list.size(); i ++){
	    		SysLogInfo sys=list.get(i);
	    		String sysLogType=sys.getSysLogInfoOperType();
     			if(sysLogType.equals("0")){
     				sysLogType="增加";
     			}
     			if(sysLogType.equals("1")){
     				sysLogType="修改";
     			}
     			if(sysLogType.equals("2")){
     				sysLogType="删除";
     			}
     			if(sysLogType.equals("3")){
     				sysLogType="登陆";
     			}
     			if(sysLogType.equals("4")){
     				sysLogType="分配权限";
     			}
     			if(sysLogType.equals("5")){
     				sysLogType="分配用户";
     			}
     			if(sysLogType.equals("6")){
     				sysLogType="分配机构范围";
     			}
     			if(sysLogType.equals("7")){
     				sysLogType="分配地区范围";
     			}
     			if(sysLogType.equals("8")){
     				sysLogType="申请";
     			}
     			if(sysLogType.equals("9")){
     				sysLogType="审批";
     			}
     			if(sysLogType.equals("10")){
     				sysLogType="退出登陆";
     			}
     			if(sysLogType.equals("11")){
     				sysLogType="立案";
     			}
     			String datatime=""+sys.getSysLogInfoOperDate();
     			sb.append(sys.getPersionFile().getPerFileName() + "   " + datatime + "   " + sysLogType + "   " + sys.getSysLogInfoOperRecord() + "\r\n");
     			
     			
	    	}
	    }
	    request.setAttribute("fileName", today + ".txt");
	    request.setAttribute("txt", sb.toString());
	    
//		try {
//		     OutputStream  os  =  null;
//		     try  {
//		             os  =  response.getOutputStream();  //取得输出流
//		             response.reset();  //清空输出流
//		             response.setHeader("Content-disposition","attachment;  filename="  +  today   +  ".xls");  //设定输出文件头
//		             response.setContentType("application/octet-stream");  //定义输出类型
//		     }  catch  (IOException  ex)  {
//		             System.out.println("流操作错误:"+ex.getMessage());
//		     }
//		     WritableWorkbook  workbook  =  null;
//		     try  {
//		             //创建新的Excel  工作簿
//		             workbook  =  Workbook.createWorkbook(os);
//		             //在Excel工作簿中建一工作表，其名为:第一页
//		             jxl.write.WritableSheet  wsheet  =  workbook.createSheet("sheet1",  0);  //sheet();
//		             WritableFont  font  =  new  WritableFont(WritableFont.ARIAL,  14,
//		                                                       WritableFont.BOLD,  false,
//		                                                       jxl.format.UnderlineStyle.
//		                                                       NO_UNDERLINE,
//		                                                       jxl.format.Colour.BLACK);
//		             WritableCellFormat  format  =  new  WritableCellFormat(font);
//		             String[] titles = {"用户姓名","操作类型","操作记录","操作时间"};
//		             wsheet.addCell(new Label(0,0,"用户姓名",format));
//		             wsheet.addCell(new Label(1,0,"操作类型",format));
//		             wsheet.addCell(new Label(2,0,"操作记录",format));
//		             wsheet.addCell(new Label(3,0,"操作时间",format));
//		             wsheet.mergeCells(0, 0, 0, 1);
//		             wsheet.mergeCells(1, 0, 1, 1);
//		             wsheet.mergeCells(2, 0, 2, 1);
//		             wsheet.mergeCells(3, 0, 3, 1);
//		             font  =  new  jxl.write.WritableFont(WritableFont.createFont("黑体"),  12,
//                             WritableFont.NO_BOLD,  false,
//                             jxl.format.UnderlineStyle.
//                             NO_UNDERLINE,
//                             jxl.format.Colour.BLACK);
//		             format  =  new  jxl.write.WritableCellFormat(font);
//		             SysLogInfoDAO dao = new SysLogInfoDAO(request, response);
//		             
//		            String begintime = request.getParameter("begintime");
//		     		String endtime = request.getParameter("endtime");
//		     		String type = request.getParameter("sysLogInfoOperType");
//		     		List<SysLogInfo> list= (List<SysLogInfo>)dao.findAll(begintime,endtime,type);
//		     		int index = 0;
//		     		for(int i=0;i<list.size();i++){
//		     			SysLogInfo sys=list.get(i);
//		     			String sysLogType=sys.getSysLogInfoOperType();
//		     			if(sysLogType.equals("0")){
//		     				sysLogType="增加";
//		     			}
//		     			if(sysLogType.equals("1")){
//		     				sysLogType="修改";
//		     			}
//		     			if(sysLogType.equals("2")){
//		     				sysLogType="删除";
//		     			}
//		     			if(sysLogType.equals("3")){
//		     				sysLogType="登陆";
//		     			}
//		     			if(sysLogType.equals("4")){
//		     				sysLogType="分配权限";
//		     			}
//		     			if(sysLogType.equals("5")){
//		     				sysLogType="分配用户";
//		     			}
//		     			if(sysLogType.equals("6")){
//		     				sysLogType="分配机构范围";
//		     			}
//		     			String datatime=""+sys.getSysLogInfoOperDate();
//		     			Label label0 = new Label(0, index+2, sys.getPersionFile().getPerFileLoginName(),  format);
//		    			wsheet.addCell(label0);
//		    			Label label1 = new Label(1, index+2, sysLogType,  format);
//		    			wsheet.addCell(label1);
//		    			Label label2 = new Label(2, index+2, sys.getSysLogInfoOperRecord(),  format);
//		    			wsheet.addCell(label2);
//		    			Label label3 = new Label(3, index+2, datatime,  format);
//		    			wsheet.addCell(label3);
//		    			
//		    			index++;
//		     		}
//		             workbook.write();  //写入文件
//		     }  catch  (WriteException  ex1)  {
//		             System.out.println("WriteException:"+ex1.getMessage());
//		     }  catch  (IOException  ex2)  {
//		             System.out.println("IOException:"+ex2.getMessage());
//		     }
//		     workbook.close();
//		     os.close();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block2
//			e.printStackTrace();
//		}
		
		return mapping.findForward("txt");
	}
}